﻿package mortal.game.scene3D.player.entity
{
    import Message.Public.*;
    import baseEngine.system.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.buff.*;
    import mortal.game.scene3D.display3d.blood.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.sunbathCopy.*;
    import mortal.game.view.copy.wine.util.*;
    import mortal.game.view.mainUI.zazen.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.task.drama.*;
    import mortal.mvc.core.*;

    public class RolePlayer extends UserPlayer
    {
        public var isCanControlWalk:Boolean = true;
        public var isCanControlAttack:Boolean = true;
        public var isCanControlReleaseMagic:Boolean = true;
        private var _isInSpasticity:Boolean = false;
        private var _scrollRect:Rectangle;
        private var _isSetPoint:Boolean = false;
        private var _serverPoints:Array;
        private const SERVERDISTANCE:Number = 60;
        private const SERVERMAXDISTANCE:Number = 80;
        protected var _isInServerMove:Boolean = false;
        protected var _isHideHead:Boolean = false;
        private var _spasticityFireKey:uint = 0;
        private static var _instance:RolePlayer;

        public function RolePlayer()
        {
            this._serverPoints = [];
            if (_instance)
            {
                throw new Error("RolePlayer 唯一性");
            }
            this._scrollRect = SceneRange.display;
            return;
        }// end function

        override protected function initPlayers() : void
        {
            super.initPlayers();
            _bodyPlayer.selectEnabled = false;
            _bodyPlayer.loadPriority = LoaderPriority.LevelA;
            var _loc_1:Boolean = false;
            _weaponPlayer.selectEnabled = false;
            return;
        }// end function

        override public function updateWing(param1:int) : void
        {
            super.updateWing(param1);
            var _loc_2:Boolean = false;
            _wingHangEffect.clearEnabled = false;
            return;
        }// end function

        override public function updateWeapons(param1:int) : void
        {
            super.updateWeapons(param1);
            var _loc_2:Boolean = false;
            _weaponHangEffect.clearEnabled = false;
            return;
        }// end function

        override public function updateMount(param1:int) : void
        {
            super.updateMount(param1);
            var _loc_2:Boolean = false;
            _mountHangEffect.clearEnabled = false;
            return;
        }// end function

        override protected function setPlayerModel(param1:int, ... args) : void
        {
            RoleResourceUtil.changeRoleModel(param1, args);
            return;
        }// end function

        override protected function setEffect(param1:EffectPlayer) : void
        {
            param1.clearEnabled = false;
            return;
        }// end function

        override public function addStateModel(param1:String, param2:Boolean = false) : void
        {
            super.addStateModel(param1, true);
            return;
        }// end function

        override protected function getBloodType() : int
        {
            if (GameMapUtil.curMapState.isWineMap)
            {
                return EBloodType.PhysicalPower;
            }
            return EBloodType.Role;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            if (_zazenEffectPlayer)
            {
                _zazenEffectPlayer.dispose();
                _zazenEffectPlayer = null;
            }
            if (_sunbathCopyPlayMusical)
            {
                _sunbathCopyPlayMusical.dispose();
                _sunbathCopyPlayMusical = null;
            }
            this.isCanControlWalk = true;
            this.isCanControlAttack = true;
            this.isCanControlReleaseMagic = true;
            this._isInSpasticity = false;
            this._isHideHead = false;
            disposeGuildWarEffect();
            BuffChainUtil.removePlayerAllChains(this);
            _isInLeading = false;
            removeTweenLite();
            return;
        }// end function

        override public function updateZazen(param1:Boolean) : void
        {
            super.updateZazen(param1);
            if (entityInfo.isInZazen)
            {
                ZazenIncomeView.instace.show();
            }
            else if (ZazenIncomeView.hasInstance())
            {
                ZazenIncomeView.instace.hide();
            }
            return;
        }// end function

        override protected function enterSwinArea() : void
        {
            super.enterSwinArea();
            Dispatcher.dispatchEvent(new DataEvent(EventName.MountRide, EMountState._EMountStateUnRide));
            return;
        }// end function

        override public function setPixlePoint(param1:int, param2:int, param3:Boolean = true, param4:Boolean = true) : void
        {
            super.setPixlePoint(param1, param2, param3, param4);
            this.setCurrentPointImpl();
            this.stopMove();
            return;
        }// end function

        public function setCurrentPointImpl(param1:Boolean = true, param2:Boolean = true) : void
        {
            if (Game.scene.isInLockScene)
            {
                return;
            }
            this._isSetPoint = false;
            var _loc_3:* = SceneRange.moveMap.x;
            var _loc_4:* = SceneRange.moveMap.y;
            if (SceneRange.map.contains(this.x2d, this.y2d))
            {
                if (param1)
                {
                    this._scrollRect.x = int(this.x2d - this._scrollRect.width / 2);
                    if (this._scrollRect.left < SceneRange.map.left)
                    {
                        this._scrollRect.x = 0;
                    }
                    if (this._scrollRect.right > SceneRange.map.right)
                    {
                        this._scrollRect.x = int(SceneRange.map.right - this._scrollRect.width);
                    }
                }
                if (param2)
                {
                    this._scrollRect.y = int(this.y2d - this._scrollRect.height / 2);
                    if (this._scrollRect.top < SceneRange.map.top)
                    {
                        this._scrollRect.y = 0;
                    }
                    if (this._scrollRect.bottom > SceneRange.map.bottom)
                    {
                        this._scrollRect.y = int(SceneRange.map.bottom - this._scrollRect.height);
                    }
                }
                Game.scene.scrollRect = this._scrollRect;
            }
            else if (Global.isDebugModle && !GameMapUtil.curMapState.isPetBreakMap)
            {
                Alert.show(Language.getString(20670));
            }
            return;
        }// end function

        public function get serverPoints() : Array
        {
            return this._serverPoints;
        }// end function

        override protected function hasNextPoint() : Boolean
        {
            if (_nextTwoPoint)
            {
                _nextPoint = _nextTwoPoint;
                _nextTwoPoint = this.cutPoint(_nextPoint._x, _nextPoint._y);
                setRealPoint(_nextPoint._x, _nextPoint._y);
                return true;
            }
            if (_nextPoint)
            {
                setRealPoint(_nextPoint._x, _nextPoint._y);
                return true;
            }
            _nextPoint = this.cutPoint(this.x2d, this.y2d);
            if (_nextPoint)
            {
                setRealPoint(_nextPoint._x, _nextPoint._y);
                return true;
            }
            return false;
        }// end function

        public function cutPoint(param1:Number, param2:Number) : AstarTurnPoint
        {
            var _loc_3:AstarTurnPoint = null;
            var _loc_4:AstarTurnPoint = null;
            var _loc_6:Point = null;
            var _loc_7:Number = NaN;
            if (_pathArray.length > 0)
            {
                _loc_3 = _pathArray[0] as AstarTurnPoint;
            }
            else
            {
                return null;
            }
            var _loc_5:* = new Point(_loc_3._x, _loc_3._y);
            if (_loc_5)
            {
                _loc_4 = new AstarTurnPoint();
                if (0)
                {
                    _loc_7 = MathUitl.getRadiansByXY(param1, param2, _loc_5.x, _loc_5.y);
                    _loc_4._x = param1 + Math.cos(_loc_7) * this.SERVERDISTANCE;
                    _loc_4._y = param2 + Math.sin(_loc_7) * this.SERVERDISTANCE;
                }
                else
                {
                    _loc_4._x = _loc_5.x;
                    _loc_4._y = _loc_5.y;
                    if (_pathArray.length > 0)
                    {
                        _pathArray.shift();
                    }
                }
                _loc_6 = GameMapUtil.getTilePoint(_loc_4._x, _loc_4._y);
                _loc_4.value = GameMapUtil.getPointValue(_loc_6.x, _loc_6.y);
                return _loc_4;
            }
            return null;
        }// end function

        override protected function isArrive() : Boolean
        {
            return speed.distance < speed.speed * 1.4;
        }// end function

        override protected function walkGridStart(param1:AstarTurnPoint) : void
        {
            var _loc_2:SPoint = null;
            super.walkGridStart(param1);
            GameManager.instance.updateLastOperTime();
            this._serverPoints.length = 0;
            if (_nextTwoPoint)
            {
                _loc_2 = new SPoint();
                _loc_2.x = _nextTwoPoint._x;
                _loc_2.y = _nextTwoPoint._y;
                this._serverPoints.push(_loc_2);
                if (!this._isInServerMove)
                {
                    FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.GIRD_WALK_START, [this]);
                }
            }
            return;
        }// end function

        public function get wizardSmallPlayer() : WizardSmallPlayer
        {
            return _wizardSmallPlayer;
        }// end function

        protected function walkStartSendServer() : void
        {
            GameManager.instance.updateLastOperTime();
            this._serverPoints.length = 0;
            var _loc_1:* = new SPoint();
            _loc_1.x = this.x2d;
            _loc_1.y = this.y2d;
            this._serverPoints.push(_loc_1);
            _loc_1 = new SPoint();
            _loc_1.x = _nextPoint._x;
            _loc_1.y = _nextPoint._y;
            this._serverPoints.push(_loc_1);
            if (_nextTwoPoint)
            {
                _loc_1 = new SPoint();
                _loc_1.x = _nextTwoPoint._x;
                _loc_1.y = _nextTwoPoint._y;
                this._serverPoints.push(_loc_1);
            }
            if (!this._isInServerMove)
            {
                FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.ServerPoint, [this]);
            }
            super.walkStart(_nextPoint);
            FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.WALK_START, [this]);
            return;
        }// end function

        private function checkIsCanWalk(param1:int, param2:int) : int
        {
            var _loc_3:* = AstarAnyDirection.mapData[param1][param2];
            if (_loc_3 > 0 && _loc_3 < 5)
            {
                return 1;
            }
            return 0;
        }// end function

        public function set inServerMove(param1:Boolean) : void
        {
            this._isInServerMove = param1;
            return;
        }// end function

        override public function walking(param1:Array) : void
        {
            checkArea();
            if (isInitInfo == false || this.isDead)
            {
                return;
            }
            if (CanRoleWalkUtil.notCanWalk)
            {
                return;
            }
            if (!entityInfo.isInFly && !GameMapUtil.curMapState.isWineMap)
            {
                entityInfo.updateStatus(EMoveStatus._EMoveStatusNormal);
            }
            if (param1.length == 0)
            {
                return;
            }
            _pathArray = param1.concat();
            _nextTwoPoint = null;
            _nextPoint = null;
            if (_isMove == false)
            {
                startMove();
                nextPathPoint(true);
                _nextTwoPoint = this.cutPoint(_nextPoint._x, _nextPoint._y);
                this.direction = _speed.direction;
                Global3D.stageFrame = AutoFightAI.isWorking ? (_pathArray.length > 0 ? (60) : (30)) : (60);
            }
            else
            {
                nextPathPoint(false);
                _nextTwoPoint = this.cutPoint(_nextPoint._x, _nextPoint._y);
                this.direction = _speed.direction;
            }
            this.walkStartSendServer();
            return;
        }// end function

        override public function stopWalking(param1:Boolean = true) : void
        {
            if (param1)
            {
                this.stopWalkSendPoint();
            }
            super.stopWalking();
            return;
        }// end function

        override public function stopMove() : void
        {
            super.stopMove();
            Global3D.stageFrame = 30;
            return;
        }// end function

        public function stopWalkSendPoint() : void
        {
            if (!isMove)
            {
                return;
            }
            GameManager.instance.updateLastOperTime();
            this._serverPoints.length = 0;
            var _loc_1:* = new SPoint();
            _loc_1.x = this.x2d;
            _loc_1.y = this.y2d;
            this._serverPoints.push(_loc_1);
            this._serverPoints.push(_loc_1);
            if (!this._isInServerMove)
            {
                FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.ServerPoint, [this]);
            }
            return;
        }// end function

        override protected function get speedCorrectPer() : Number
        {
            return 1;
        }// end function

        override protected function walkGridEnd() : void
        {
            super.walkGridEnd();
            if (_nextPoint)
            {
                this.updateSceneXY();
            }
            FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.GIRD_WALK_END, [this]);
            return;
        }// end function

        override protected function updateIsInSafeArea() : void
        {
            ThingUtil.isNameChange = true;
            return;
        }// end function

        public function walkBreak() : void
        {
            this.stopMove();
            FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.WALK_BREAK, [this]);
            return;
        }// end function

        override protected function walkEnd() : void
        {
            super.walkEnd();
            FrEventDispatcher.instance.proxyDispatchEvent(this, PlayerEvent.WALK_END, [this]);
            return;
        }// end function

        override protected function onMove() : void
        {
            super.onMove();
            ThingUtil.isMoveChange = true;
            this.updateSceneXY();
            return;
        }// end function

        private function updateSceneXY() : void
        {
            var _loc_1:Rectangle = null;
            var _loc_2:Rectangle = null;
            var _loc_3:Boolean = false;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            if (Game.scene.isInLockScene)
            {
                return;
            }
            this.setCurrentPointImpl();
            return;
        }// end function

        override public function set isDead(param1:Boolean) : void
        {
            if (param1 != _isDead)
            {
                super.isDead = param1;
                if (param1 == false)
                {
                    Log.debug("人物复活");
                    this.isCanControlWalk = true;
                    this.isCanControlReleaseMagic = true;
                    this.isCanControlAttack = true;
                    this.stopMove();
                }
                else
                {
                    Log.debug("人物躺下");
                    this.stopMove();
                    ThingUtil.selectEntity = null;
                }
            }
            return;
        }// end function

        override public function hoverTest(param1:Number, param2:Number, param3:Number, param4:Number) : Boolean
        {
            return false;
        }// end function

        public function setHideHead(param1:Boolean) : void
        {
            this._isHideHead = param1;
            this.updateHeadContainer();
            return;
        }// end function

        override public function updateHeadContainer() : void
        {
            var _loc_1:* = entityInfo.isInLayer && !this._isHideHead;
            var _loc_2:* = SystemSetting.instance.isHideTitle.bValue;
            var _loc_3:* = SystemSetting.instance.isHideGuildName.bValue;
            _headContainner.updateBloodVisible(_loc_1 && !GameMapUtil.isInHideBloodMap);
            _headContainner.updateNameVisible(_loc_1 && !GameMapUtil.isInHideNameMap);
            _headContainner.updateGuildVisible(_loc_1 && !GameMapUtil.isInHideGuildMap && !_loc_3);
            _headContainner.updateTitleVisible(_loc_1 && !GameMapUtil.isInHideTitleMap && !_loc_2);
            return;
        }// end function

        public function spasticityFire() : void
        {
            this.spasticityBeginFight(500);
            return;
        }// end function

        public function spasticityBeginFight(param1:int) : void
        {
            clearTimeout(this._spasticityFireKey);
            this.isInSpasticity = true;
            this._spasticityFireKey = setTimeout(this.clearSpasticity, param1);
            return;
        }// end function

        public function clearSpasticity() : void
        {
            this.isInSpasticity = false;
            return;
        }// end function

        public function get isInSpasticity() : Boolean
        {
            return this._isInSpasticity;
        }// end function

        public function set isInSpasticity(param1:Boolean) : void
        {
            this._isInSpasticity = param1;
            if (param1 == false)
            {
                FrEventDispatcher.instance.proxyDispatchEvent(this, RolePlayerEvent.SpasticityClear);
            }
            return;
        }// end function

        override protected function updateFly(param1:Boolean, param2:Boolean) : void
        {
            super.updateFly(param1, param2);
            if (param2)
            {
                if (param1)
                {
                    GameController.scene.flyToSky(0, 1.5);
                }
                else
                {
                    GameController.scene.land(0);
                }
            }
            else if (param1)
            {
                if (!TaskDramaOperator.isInFlyDrama)
                {
                    GameController.scene.flyToSky(2, 1.5);
                }
            }
            return;
        }// end function

        override protected function tweenFlyY(param1:Boolean) : void
        {
            if (!TaskDramaOperator.isInFlyDrama)
            {
                super.tweenFlyY(param1);
            }
            return;
        }// end function

        override public function updateDance(param1:Boolean) : void
        {
            super.updateDance(param1);
            if (param1)
            {
                SunbathSpeakUtil.speakByType(SunbathSpeakUtil.dance);
            }
            return;
        }// end function

        override public function updatePlayMusical(param1:Boolean) : void
        {
            super.updatePlayMusical(param1);
            SunbathUtil.isInPerFormNow = param1;
            if (param1)
            {
                SunbathSpeakUtil.speakByType(SunbathSpeakUtil.perform);
            }
            return;
        }// end function

        override public function updateDrunk(param1:Boolean) : void
        {
            super.updateDrunk(param1);
            if (param1)
            {
                WinePartySpeakUtil.speakByType(WinePartySpeakUtil.drunk);
            }
            return;
        }// end function

        override public function updateMoraReady(param1:Boolean) : void
        {
            if (param1)
            {
                WinePartySpeakUtil.speakByType(WinePartySpeakUtil.mora);
            }
            return;
        }// end function

        public static function get instance() : RolePlayer
        {
            if (!_instance)
            {
                _instance = new RolePlayer;
            }
            return _instance;
        }// end function

        public static function hasInstacne() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
